/*
 * Copyright (c) 2015—2030 GantSoftware.Co.Ltd. All rights reserved.
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * is not allowed to be distributed or copied without the license from
 * GantSoftware.Co.Ltd. Please contact the company for more information.
 */
package com.gantang.gax.dto;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.gantang.gax.constant.CommonConstant;
import com.gantang.gax.enums.ResultFailEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class ResultDto implements Serializable {
    @JsonProperty("success")
    private String success = CommonConstant.RESULT_SUCCESS_TRUE;
    private String errorCode;
    private String errorDesc;
    private String reqId;

    public ResultDto(String success) {
        this.success = success;
    }

    public ResultDto(String success, String errorCode, String errorDesc) {
        this.success = success;
        this.errorCode = errorCode;
        this.errorDesc = errorDesc;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return CommonConstant.RESULT_SUCCESS_TRUE.equals(getSuccess());
    }

    @JsonIgnore
    public boolean isFail() {
        return CommonConstant.RESULT_SUCCESS_FALSE.equals(getSuccess());
    }

    @JsonIgnore
    public static ResultDto success() {
        return new ResultDto();
    }

    @JsonIgnore
    public static ResultDto fail(String errorCode, String errorDesc) {
        ResultDto resultDto = new ResultDto();
        resultDto.setSuccess(CommonConstant.RESULT_SUCCESS_FALSE);
        resultDto.setErrorCode(errorCode);
        resultDto.setErrorDesc(errorDesc);
        return resultDto;
    }

    @JsonIgnore
    public static ResultDto fail(ResultFailEnum resultFailEnum, String errorDesc) {
        ResultDto resultDto = new ResultDto();
        resultDto.setSuccess(CommonConstant.RESULT_SUCCESS_FALSE);
        resultDto.setErrorCode(resultFailEnum.getErrorCode());
        if (StrUtil.isEmpty(errorDesc)) {
            resultDto.setErrorDesc(resultFailEnum.getErrorDesc());
        } else {
            resultDto.setErrorDesc(resultFailEnum.getErrorDesc() + "[" + errorDesc + "]");
        }
        return resultDto;
    }
}
